﻿using System;
using System.Linq;
using System.Net.Mail;
using System.Web.Mvc;
using System.Web.Security;
using MvcMembership;
using CoinDev.Areas.UserAdministration.Models.UserAdministration;
using System.IO;
using Newtonsoft.Json;
using CoinDev.Models.JSON;
using CoinDev.Areas.UserAdministration.Models;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Web;

namespace CoinDev.Areas.UserAdministration.Controllers
{
    //[Authorize(Roles = "Site Administrator")]
    public class CategoryController : Controller
    {
        public ActionResult Index()
        {
            ArrayList paramList = new ArrayList
            {
            };

            var dtGetCategories = Data.Connections.GetDataTable("Admin_Get_CategoryList", paramList, "ApplicationServices");

            var Categories = new CoinDev.Models.JSON.CategoriesArray { };

            foreach (DataRow drGetCategories in dtGetCategories.Rows)
            {
                Categories.Category.Add(new CoinDev.Models.JSON.Categories
                {
                    Id = drGetCategories["Id"].ToString(),
                    Name = drGetCategories["Name"].ToString(),
                    Year = drGetCategories["Year"].ToString()
                });
            }

            return View("List", Categories);
        }

        public ActionResult Add()
        {
            return View("Add");
        }
        public ActionResult Edit()
        {
            try
            {
                ArrayList paramList = new ArrayList
                {
                    new SqlParameter("Id", Request.Params["Id"])
                };

                var dtCategoryInfo = CoinDev.Data.Connections.GetDataTable("Admin_Get_CategoryById", paramList, "ApplicationServices");

                var Categories = new CoinDev.Models.JSON.CategoriesArray { };

                foreach (DataRow drGetCategories in dtCategoryInfo.Rows)
                {
                    Categories.Category.Add(new CoinDev.Models.JSON.Categories
                    {
                        Id = drGetCategories["Id"].ToString(),
                        Name = drGetCategories["Name"].ToString(),
                        Description = drGetCategories["Description"].ToString(),
                        Year = drGetCategories["Year"].ToString(),
                        PictureLocation = drGetCategories["Picture"].ToString()
                    });
                }
                return View("Edit", Categories);
            }
            catch
            {
                var Check = new CoinDev.Areas.UserAdministration.Models.JSON.SubmissionCheck
                {
                    Check = "0",
                    Message = "No category",
                };
                return View("Edit");
            }
        }

        public ActionResult NewCategory()
        {
            var sr = new StreamReader(Request.InputStream);
            var contentString = sr.ReadToEnd();
            var parameterobjects = JsonConvert.DeserializeObject<Categories>(contentString);
            var message = "";
            var check = "1";

            if (parameterobjects.PictureLocation == "")
            {
                return new EmptyResult();
            }
            else
            {
                ArrayList paramList = new ArrayList
                {
                    new SqlParameter("Name", parameterobjects.Name),
                    new SqlParameter("Year", parameterobjects.Year),
                    new SqlParameter("Description", parameterobjects.Description),
                    new SqlParameter("Picture", "../../Content/Images/Category/" + parameterobjects.PictureLocation)
                };

                var drInsertCommissionAdjustment = CoinDev.Data.Connections.GetDataRow("Admin_Insert_Category", paramList, "ApplicationServices");

                Session["Id"] = Convert.ToInt32(drInsertCommissionAdjustment["Id"].ToString());
                message = drInsertCommissionAdjustment["message"].ToString();
                check = drInsertCommissionAdjustment["check"].ToString();
                Session["New"] = 1;

                var SubmissionCheck = new CoinDev.Areas.UserAdministration.Models.JSON.SubmissionCheck
                {
                    Check = check,
                    Message = message
                };

                return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);
            }

        }
        public ActionResult EditCategory()
        {
            var sr = new StreamReader(Request.InputStream);
            var contentString = sr.ReadToEnd();
            var parameterobjects = JsonConvert.DeserializeObject<Categories>(contentString);
            var message = "";
            var check = "1";

            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", parameterobjects.Id),
                new SqlParameter("Name", parameterobjects.Name),
                new SqlParameter("Year", parameterobjects.Year),
                new SqlParameter("Description", parameterobjects.Description)
            };

            var drInsertCommissionAdjustment = CoinDev.Data.Connections.GetDataRow("Admin_Edit_Category", paramList, "ApplicationServices");

            message = drInsertCommissionAdjustment["message"].ToString();
            check = drInsertCommissionAdjustment["check"].ToString();

            var SubmissionCheck = new CoinDev.Areas.UserAdministration.Models.JSON.SubmissionCheck
            {
                Check = check,
                Message = message
            };

            return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);

        }

        public ActionResult DeleteCategory(int Id)
        {
            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", Id)
            };

            var drDeleteCategory = CoinDev.Data.Connections.GetDataRow("Admin_Delete_Category", paramList, "ApplicationServices");

            var SubmissionCheck = new CoinDev.Areas.UserAdministration.Models.JSON.SubmissionCheck
            {
                Check = drDeleteCategory["check"].ToString(),
                Message = drDeleteCategory["message"].ToString()
            };

            return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);

        }
        public ActionResult DisableCategory(int Id)
        {
            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", Id)
            };

            var drDeleteCategory = CoinDev.Data.Connections.GetDataRow("Admin_Disable_Category", paramList, "ApplicationServices");

            var SubmissionCheck = new CoinDev.Areas.UserAdministration.Models.JSON.SubmissionCheck
            {
                Check = drDeleteCategory["check"].ToString(),
                Message = drDeleteCategory["message"].ToString()
            };

            return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);

        }

        public ActionResult UploadPictureCategory()
        {
            try
            {
                foreach (string file in Request.Files)
                {
                    var postedFile = Request.Files[file];
                    postedFile.SaveAs(Server.MapPath("~/Content/Images/Category/") + Path.GetFileName(postedFile.FileName));
                }
                return Redirect("Edit?Id=" + Convert.ToInt32(Session["Id"]));
            }
            catch
            {
                return Redirect("Add?Id=0");
            }
        }

    }
}
